<template>
  <div class="box1">
    <h1>会员管理，展示会员页面</h1>
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="会员手机">
        <el-input v-model.number="value" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="dialogFormVisible = true"
          >新增会员</el-button
        >
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="会员id" width="100"> </el-table-column>
      <el-table-column prop="vip_name" label="会员姓名" width="160">
      </el-table-column>
      <el-table-column prop="vip_discount" label="会员折扣" width="160">
      </el-table-column>
      <el-table-column
        prop="vip_total_consume"
        label="会员累计消费金额"
        width="160"
      >
      </el-table-column>
      <el-table-column prop="vip_phone" label="会员电话" width="180">
      </el-table-column>
      <el-table-column prop="address" label="修改信息">
        <template slot-scope="scope">
          <el-button type="text" @click="modvip(scope.row)">修改</el-button>
          <el-button type="text" @click="delvip(scope.row)">删除会员</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="fenye">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="10"
      ></el-pagination>
    </div>
    <el-dialog title="添加会员" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="会员姓名" :label-width="formLabelWidth">
          <el-input v-model="form.vip_name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form">
        <el-form-item label="会员电话" :label-width="formLabelWidth">
          <el-input v-model="form.vip_phone" auto-complete="off"></el-input>
        </el-form-item> </el-form
      ><el-form :model="form">
        <el-form-item label="会员折扣" :label-width="formLabelWidth">
          <el-input v-model="form.vip_discount" auto-complete="off"></el-input>
        </el-form-item> </el-form
      ><el-form :model="form">
        <el-form-item label="会员消费金额" :label-width="formLabelWidth">
          <el-input
            v-model="form.vip_total_consume"
            auto-complete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="vipXinzeng">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  vipshuju,
  vipxinzeng,
  vipshanchu,
  vipxiugai,
  vipchaxun,
} from "../utils/api/member";
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "",
  components: {},
  data() {
    return {
      value: null,
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: null,
        vip_name: null,
        vip_phone: null,
        vip_discount: null,
        vip_total_consume: null,
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.vipshuju();
  },
  methods: {
    //获取数据
    vipshuju() {
      vipshuju().then((data) => {
        // console.log(data);
        this.tableData = data.data.data;
      });
    },
    //新增函数
    vipXinzeng() {
      //判断点击新增的弹框是否有id
      //没有就弹出空的表单
      if (this.form.id) {
        vipxiugai(this.form).then((data) => {
          // console.log(data);
          this.vipshuju();
        });
        //有就弹出有数据的表单
      } else {
        vipxinzeng(this.form).then((data) => {
          // console.log(data);
          if (data.data.code == 200) {
            this.vipshuju();
          }
        });
      }
      this.form = {
        id: null,
        vip_name: null,
        vip_phone: null,
        vip_discount: null,
        vip_total_consume: null,
      };
      this.dialogFormVisible = false;
    },
    //删除
    delvip(obj) {
      // console.log(obj);
      let id = obj.id;
      vipshanchu(id).then((data) => {
        // console.log(data);
        if (data.data.code == 200) {
          this.vipshuju();
        }
      });
    },
    //修改
    modvip(obj) {
      this.dialogFormVisible = true;
      //把内容装到form表单里
      this.form = { ...obj };
    },

    //搜索
    search() {
      if (this.value != "") {
        vipchaxun(this.value).then((data) => {
          // console.log(data);
          this.tableData = data.data.data;
        });
      } else {
        this.vipshuju();
      }
    },
  },
  //监听
  watch: {
    dialogFormVisible(val) {
      if (val == false) {
        this.form = {
          id: null,
          vip_name: null,
          vip_phone: null,
          vip_discount: null,
          vip_total_consume: null,
        };
      }
    },
  },
  mounted() {
    this.vipshuju();
  },
};
</script>
<style lang="less" scoped>
.box1 {
  padding-left: 5%;
}
</style>